/**
 * Created by chenzp on 18-8-17.
 */
layui.define(['jquery','layer','utl'],function (exports) {


    var $ = layui.$

    var utl = layui.utl

    //所有ly-html缓存
    var allLyIndexs = []


    var allLy = []

    /**
     * 根据 index获取缓存中的ly对象
     *
     * @param index
     * @returns {*}
     */
    function lyObj(index) {
        for(var i=0;i<allLy.length;i++){
            var lyObj = allLy[i]
            if(lyObj.index == index) {
                return lyObj
            }
        }
    }

    /**
     * 根据 目标对象获取ly对象
     *
     * @param target
     * @returns {*}
     */
    function lyObjByJq(target) {

        for(var i=0;i<allLy.length;i++){
            var lyObj = allLy[i]
            if(lyObj.target.is(target)) {
                return lyObj
            }
        }
    }

    /**
     * 根据 index删除ly对象
     * @param index
     */
    function deleteLyByIndex(index) {
        for(var i=0;i<allLy.length;i++){
            var lyObj = allLy[i]
            if(lyObj.index == index) {
                lyObj.$jq.hide()
                layer.close(index)
                allLy.splice(i,1)
                break
            }
        }
    }


    /**
     * 根据 index删除ly对象
     * @param target
     */
    function deleteLyByJq(target) {
        for(var i=0;i<allLy.length;i++){
            var lyObj = allLy[i]
            if(lyObj.target.is(target)) {
                lyObj.$jq.hide()
                layer.close(lyObj.index)
                allLy.splice(i,1)
                break
            }
        }
    }

    /**
     * 弹出html框
     * @param target
     * @param option
     */
    layui.jquery.fn.openLy = function (target, event, option) {

         var $jq = utl.jq(target)

        if (event && typeof event == 'string') {
            $(this).on(event, function () {opLy(this)})
            return
        } else {
            opLy(this)
        }

        /**
         * 打开窗口
         */
        function opLy(self) {
            var area = []
            if (option.area) {
                var area_arr = option.area.split('*')

                if (area_arr.length != 2) {
                    layer.msg('弹出框宽高格式不正确')
                    return
                }
                area[0] = area_arr[0].endsWith('%') ? area_arr[0] : area_arr[0] + 'px'
                area[1] = area_arr[1].endsWith('%') ? area_arr[1] : area_arr[1] + 'px'
            }

            var newOption = $.extend({}, {
                 type: 1
                ,content: $jq
                ,cancel: function (index, layero) {
                    deleteLyByIndex(index)
                }
                ,success:function (layero, index) {
                    allLy.push({index:index,target:$(self),$jq:$jq})
                }

            }, option, {area: area})

            layer.open(newOption)
        }
    }


    /**
     * 关闭html弹出层
     * @constructor
     */
    layui.jquery.fn.closeLy = function (index) {

        var $this = $(this)

        if (!index) {
            deleteLyByJq($(this))
        }else{
            deleteLyByIndex(index)
        }


    }

    /**
     * 关闭全部的lay-html弹出框
     * @constructor
     */
    var closeAllLy = function () {
        for(var i=0;i<allLy.length;i++){
            var lyObj = allLy[i]
            layer.close(lyObj.index)
        }

        allLy = []
    }


    exports('ly',{close:closeAllLy})
})